import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Router, NavigationEnd, NavigationStart } from '@angular/router';
import { Pagination } from '../../../../public/serve/pcrudInfo';
import { Service } from '../../../../server/server';
import { CudTableService } from '../../../../public/serve/cud-table/cud-table.service';
import { RemoveTableService } from '../../../../public/serve/remove-table/remove-table.service';
declare var $: any;

@Component({
  selector: 'app-messagecontract',
  templateUrl: './messagecontract.component.html'
})
export class MessagecontractComponent implements OnInit {

  constructor(
    private server: Service,
    private tableListService: CudTableService,
    public removeTableService: RemoveTableService,
    public router: Router
  ) { }

  ngOnInit() {
    this.userId = this.server.getuser();
    // this.cancelEimt = this.websocketService.hornClick.subscribe((value: any) => {
    //   this.getTabList(1, 20, this.userId);
    //   this.pagination.pageNumber = 1;
    // });
  }

  //取消订阅
  public cancelEimt: any;
  ngOnDestroy() {
    if (this.cancelEimt) {
      this.cancelEimt.unsubscribe()
    }
  }

  public totalItems: number;

  public userId: any;
  public tabVal: any = false;
  public counts: any = 0;

  @Output() messageOutput = new EventEmitter<string>();

  // 表格合同
  public getTabList(pageNumber, pageSize, userId) {
    let url = this.server.baseUrl + 'Service/Contract/getContractPushMessage';
    let data = { pageNumber: pageNumber, pageSize: pageSize, userId: userId}
    this.tableListService.postRequest(url, data).then((data: any) => {
      this.totalItems = data.length;
      this.tabVal = data.result;
      //this.messageOutput.emit(data.length);
      this.server.messageContract.emit(data.length);
      //this.server.setcounts(data.length);
      console.log(data);
    }).catch();
  }

  //修改消息状态 state=0修改状态 state=1删除
  public updatePushStatus(val, opType) {
    //console.log(val,state)
    let url = this.server.baseUrl + 'Service/Contract/updateContractPushMessages';
    let id;
    if (val) {
      id = [val.pushMessageId];
    } else {
      id = this.removeTableService.getRemoveTableId;
    }
    let data = {
      pushMessageIds: id,
      opType: opType,
    }
    //console.log(data);
    this.tableListService.postRequest(url, data).then((data: any) => {

      this.cancelSelectAll();
      this.getTabList(1, 20, this.userId);
      if (val) {
        $("#message").modal("hide");
        this.router.navigate(['/order/contract'], {
          // queryParams: {
          //   Id: val.orderProcesses.institutionId,
          //   status: val.orderProcesses.orderStatus,
          // }
        });
      }

    })
  }

  tableNumber: number = 0;
  // 当前页（默认为第一页）
  private pageNumber: number = 1;
  // 每页显示多少条数据（默认每页显示20条）
  private pageSize: number = 20;
  // id
  private id: string = '0';
  // 分页
  // 分页
  public pagination: Pagination = {
    id: '',
    pageNumber: 1,
    pageSize: 20
  };
  isLoadTableFromPaginationHandler(pagination: Pagination) {
    this.pagination = pagination;

    this.pageNumber = this.pagination[0].pageNumber;
    this.pageSize = this.pagination[0].pageSize;
    this.id = this.pagination[0].id;

    this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;
    this.getTabList(this.pageNumber, this.pageSize, this.userId);
    this.cancelSelectAll();

  }


  //批量
  isSelectAll: boolean = false;
  isChose: boolean = false;
  isSelect: boolean = true;
  isDisable: any;              // 按钮（启用/禁用）

  /**
 * 全选
 * @param bool 
 * @param tabVal 
 */
  selectAll(bool, tabVal) {
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'pushMessageId');
    this.isSelect = !this.isSelectAll;
  }

  /**
   * 单选（多选）
   * @param bool 
   * @param functionId 
   * @param length 
   */
  chose(bool, pushMessageId, length) {
    this.isDisable = this.removeTableService.removeChose(bool, pushMessageId, length);
    this.isSelectAll = this.isDisable.bool;
    this.isDisable.len ? this.isSelect = false : this.isSelect = true;
  }

  /**
   * 没有数据时取消勾选全选按钮
   */
  cancelSelectAll() {
    this.isSelectAll = false;
    this.isChose = false;
    this.removeTableService.getRemoveTableId = [];
  }


}
